//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.indexes.goodscouponlist;

import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.DateTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.TableShell;
import com.nebutown.commonunits.dataface.definitions.ButtonField;
import com.nebutown.commonunits.dataface.definitions.FieldValue;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.dataface.definitions.PageInfo;
import com.nebutown.commonunits.dataface.definitions.TimeInterval;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.restaurant.application.indexes.goodscouponlist.mysql.GoodsCouponListBean;
import com.restaurant.application.indexes.goodscouponlist.mysql.GoodsCouponListCriteria;
import com.restaurant.application.indexes.goodscouponlist.mysql.GoodsCouponListImpl;
import com.restaurant.application.indexes.goodscouponlist.support.AbstractAssistGoodsCouponList;
import com.restaurant.application.indexes.goodscouponlist.support.GoodsCouponSearch;
import com.restaurant.application.indexes.goodscouponlist.support.PadGoodsCouponSearch;
import com.restaurant.application.minifacade.data.CouponListPadBO;
import com.restaurant.application.minifacade.data.CouponPadBO;
import com.restaurant.application.util.CommonUtils;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class AssistGoodsCouponListImpl extends AbstractAssistGoodsCouponList {
    public AssistGoodsCouponListImpl() {
    }

    public ViewUpdate doSearch(GoodsCouponSearch doSearch, int[] seq) {
        return this.returnView(new View[]{this.newCommonTable(doSearch)});
    }

    public View getFullView() {
        return this.newCommonTable(new GoodsCouponSearch());
    }

    public View getViewLead(ViewSpec spec) {
        return null;
    }

    private View newCommonTable(GoodsCouponSearch search) {
        int pageNum = 1;
        if (Objects.nonNull(search.getPage()) && search.getPage() > 1L) {
            pageNum = search.getPage().byteValue();
        }

        this.getSearchSql(search);
        int totalRecord = ((GoodsCouponListImpl)this.getIndex(GoodsCouponListImpl.class)).count(this.getSearchSql(search));
        int pageSize = 10;
        int start = (pageNum - 1) * pageSize;
        int stop = pageNum * pageSize;
        int totalPage = (totalRecord + pageSize - 1) / pageSize;
        List<GoodsCouponListBean> search1 = ((GoodsCouponListImpl)this.getIndex(GoodsCouponListImpl.class)).search(this.getSearchSql(search), CommonUtils.getSort("CREATETIME"), start, stop);
        PageInfo pageInfo = new PageInfo(Integer.valueOf(pageSize), Integer.valueOf(pageNum), totalPage, totalRecord, this.getPath(CMD_DOSEARCH, new int[0]), "page");
        PageForm pageForm = this.newFormShell(search, search1, pageInfo).getForm();
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), pageForm);
    }

    private GoodsCouponListCriteria getSearchSql(GoodsCouponSearch search) {
        if (Objects.isNull(search.getFirstTime())) {
            search.withFirstTime(new TimeInterval());
        }

        if (Objects.isNull(search.getUsedTime())) {
            search.withUsedTime(new TimeInterval());
        }

        return (new GoodsCouponListCriteria()).withNameLike(search.getName()).withCouponNumberLike(search.getNumber()).withUsefulEquals(search.getUseful()).withStateEquals(search.getState()).withCreateTimeLower(search.getFirstTime().getFrom()).withCreateTimeUpper(search.getFirstTime().getTo()).withFirstCustomerIdLike(search.getFirstCustomerId()).withFirstPhoneLike(search.getFirstPhone()).withUsedTimeLower(search.getUsedTime().getFrom()).withUsedTimeUpper(search.getUsedTime().getTo()).withUsedCustomerIdLike(search.getUsedCustomerId()).withUsedPhoneLike(search.getUsedPhone()).withTypeEquals("到店自提");
    }

    private FormShell newFormShell(GoodsCouponSearch search, List<GoodsCouponListBean> beans, PageInfo info) {
        FormShell shell = new FormShell("优惠券/商品使用记录", 3, 6);
        shell.source(search, DataModes.EDIT);
        shell.at(0, 0, "券名").large(1, 1).stringField("name", search.getName()).placeHolder("券名").setEditable(true);
        shell.at(0, 1, "券号").large(1, 1).stringField("number", search.getNumber()).placeHolder("券号").setEditable(true);
        shell.at(0, 2, "优惠券状态").large(1, 1).selectEntry("useful", search.getUseful(), Lists.newArrayList(new String[]{"有效", "无效"})).placeHolder("优惠券状态").setEditable(true);
        shell.at(1, 0, "使用状态").large(1, 1).selectEntry("state", search.getState(), Lists.newArrayList(new String[]{"已使用", "未使用"})).placeHolder("使用状态").setEditable(true);
        shell.at(1, 1, "领用时间").large(1, 1).intervalField("firstTime", search.getFirstTime(), DateTypes.DATETIME).placeHolder("领用时间").setEditable(true);
        shell.at(1, 2, "使用时间").large(1, 1).intervalField("usedTime", search.getUsedTime(), DateTypes.DATETIME).placeHolder("使用时间").setEditable(true);
        shell.at(2, 0, "领用人ID").large(1, 1).stringField("firstCustomerId", search.getFirstCustomerId()).placeHolder("领用人ID").setEditable(true);
        shell.at(2, 1, "领用人手机").large(1, 1).stringField("firstPhone", search.getFirstPhone()).placeHolder("领用人手机").setEditable(true);
        shell.at(3, 0, "使用人ID").large(1, 1).stringField("usedCustomerId", search.getUsedCustomerId()).placeHolder("使用人ID").setEditable(true);
        shell.at(3, 1, "使用人手机").large(1, 1).stringField("usedPhone", search.getUsedPhone()).placeHolder("使用人手机").setEditable(true);
        List<ButtonField> buttons = Lists.newArrayList();
        buttons.add(new ButtonField(this.getPath(CMD_DOSEARCH, new int[0]), "查询", true, "", false));
        shell.at(3, 2, (String)null).buttonBoxField((ButtonField[])buttons.toArray(new ButtonField[0]));
        TableShell<GoodsCouponListBean> tableShell = new TableShell(GoodsCouponListBean.class, beans);
        tableShell.createColumn("createTime").withWidth(160).withName("领取时间").cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(DateTimeUtils.toTime(bean.getCreateTime()));
        });
        tableShell.createColumn("firstCustomerId").withName("用户ID").cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getFirstCustomerId());
        });
        tableShell.createColumn("firstPhone").withWidth(120).withName("手机").cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getFirstPhone());
        });
        tableShell.createColumn("type").withName("类型").withWidth(70).cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue("商品");
        });
        tableShell.createColumn("name").withName("券名").cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getName());
        });
        tableShell.createColumn("couponNumber").withName("券号").withWidth(130).cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getCouponNumber());
        });
        tableShell.createColumn("useful").withName("优惠券状态").withWidth(70).cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getUseful());
        });
        tableShell.createColumn("state").withName("使用状态").withWidth(80).cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getState());
        });
        tableShell.createColumn("usedTime").withName("使用日期").withWidth(160).cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getUsedTime() > 0L ? DateTimeUtils.toTime(bean.getUsedTime()) : "");
        });
        tableShell.createColumn("usedCustomerId").withName("使用人ID").cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getUsedCustomerId());
        });
        tableShell.createColumn("usedPhone").withName("使用人手机").withWidth(120).cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getUsedPhone());
        });
        tableShell.createColumn("cashierName").withName("核销人").withWidth(90).cellCalculator((e) -> {
            GoodsCouponListBean bean = (GoodsCouponListBean)e;
            return (new FieldValue()).withValue(bean.getCashierName());
        });
        tableShell.withPageInfo(info);
        shell.at(5, 0, "优惠券/商品使用记录").large(3, 3).tableField("couponList", tableShell);
        return shell;
    }

    public CouponListPadBO getCouponListPadBO(PadGoodsCouponSearch searchData) {
        List<GoodsCouponListBean> beanList = ((GoodsCouponListImpl)this.getIndex(GoodsCouponListImpl.class)).search((new GoodsCouponListCriteria()).withUsedTimeLower(searchData.getUsedStartTime()).withUsedTimeUpper(searchData.getUsedEndTime()).withNameLike(searchData.getModality()).withCouponNumberLike(searchData.getNumber()).withUsedPhoneLike(searchData.getPhone()), CommonUtils.getSort("CREATETIME"), (searchData.getPage() - 1) * searchData.getPageSize(), searchData.getPage() * searchData.getPageSize());
        List<CouponPadBO> collect = (List)beanList.stream().map((e) -> {
            return (new CouponPadBO()).withName(e.getName()).withNumber(e.getCouponNumber()).withUsedPhone(e.getUsedPhone()).withUsedTime(Objects.nonNull(e.getUsedTime()) && e.getUsedTime() > 0L ? DateTimeUtils.toTime(e.getUsedTime()) : "");
        }).collect(Collectors.toList());
        int count = ((GoodsCouponListImpl)this.getIndex(GoodsCouponListImpl.class)).count((new GoodsCouponListCriteria()).withUsedTimeLower(searchData.getUsedStartTime()).withUsedTimeUpper(searchData.getUsedEndTime()).withNameLike(searchData.getModality()).withCouponNumberLike(searchData.getNumber()).withUsedPhoneLike(searchData.getPhone()));
        return (new CouponListPadBO()).withLists(collect).withTotalRecord((long)count);
    }
}
